// Copyright (c) 2025 Huawei Technologies Co., Ltd.
// openUBMC is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//         http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Description: 调用kvm客户端认证模块的例子
#include <cassert>
#include <exception>
#include <iostream>
#include "AuthenticationManager.hpp"
const int callTimes = 5;

std::string host, port, userName, kvmMode;
char passWord[256];

int TestAuth(AuthenticationManager& mgr)
{
    auto create_res = mgr.HttpCreateSession(host.c_str(), port.c_str(), userName.c_str(), passWord, kvmMode.c_str());
    if (!create_res) {
        return -1;
    }
    auto info = mgr.GetCreateSessionResultInfo();
    if (const HttpSuccInfo* pSuccType = boost::get<HttpSuccInfo>(&info)) {
        std::cout << "Create Session GetSuccInfo:" << std::endl;
        std::cout << "---- title: " << pSuccType->title << std::endl;
        std::cout << "---- sessionId: " << pSuccType->sessionId << std::endl;
        std::cout << "---- kvmIp: " << pSuccType->kvmIp << std::endl;
        std::cout << "---- token: " << pSuccType->token << std::endl;
        std::cout << "---- kvmPort: " << pSuccType->kvmPort << std::endl;
        std::cout << "---- privilege: [ " << std::endl;
        for (auto e : pSuccType->privilege) {
            std::cout << "******** :" << e << std::endl;
        }
        std::cout << "]" << std::endl;
    } else if(const HttpFailInfo* pFailType = boost::get<HttpFailInfo>(&info)) {
        std::cout << "Create Session GetFailInfo:" << std::endl;
        std::cout << "---- errorCode: " << pFailType->errorCode << std::endl;
        std::cout << "---- message: " << pFailType->message << std::endl;
    } else {
        std::cout << "Unknown type" << std::endl;
    }
    auto deleteRes = mgr.HttpDeleteSession(host.c_str(), port.c_str(), userName.c_str(), passWord);
    if (!deleteRes) {
        return -1;
    }
    std::cout << "Delete Call:" << mgr.GetDeleteStatus() << std::endl;
    return 0;
}

int main()
{
    try {
        std::cout << "Enter host:";
        std::cin >> host;
        std::cout << "Enter port:";
        std::cin >> port;
        std::cout << "Enter userName:";
        std::cin >> userName;
        std::cout << "Enter passWord:";
        std::cin >> passWord;
        std::cout << "Enter kvmMode:";
        std::cin >> kvmMode;
        std::cout << "====== Begin test ======" << std::endl;
        AuthenticationManager mgr;
        std::cout << "====== create mgr succ ======" << std::endl;
        for (int i = 0; i < callTimes; ++i) {
            if (TestAuth(mgr) != 0) {
                std::cout << "========================= test error ===============================" << std::endl;
                throw std::exception("test error");
            }
        }
        std::cout << "====== end test ======" << std::endl;
        return 0;
    } catch (const std::exception& e) {
        std::cout << "auth test exception:" << e.what() << std::endl;
        throw e;
    }
}